﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;
using CarLoanSystem.DAL;

namespace CarLoanSystem
{
    public partial class frmCusDept : Form
    {
        private static CustomerDebtItem cusDeptItem;
        private static string mode;
        private static int CusID;
        public frmCusDept(int id,string tmpMode)
        {
            InitializeComponent();
            mode = tmpMode;
            if (mode.Equals("Add"))
            {
                CusID = id;
                this.cbbDeptStatus.Enabled = false;
                this.cbbDeptStatus.SelectedIndex = 0;
            }
            else if(mode.Equals("Edit"))
            {
                cusDeptItem = CustomerDebtInfoDAL.LoadCusDept(id);
                this.txtAmount.Text = cusDeptItem.DEBT_AMOUNT.ToString();
                this.txtDetail.Text = cusDeptItem.DEBT_DETAIL;

                if (cusDeptItem.CUSTOMER_DEBT_STATUS.Equals("A"))
                {
                    this.cbbDeptStatus.SelectedIndex = 0;
                }
                else 
                {
                    this.cbbDeptStatus.SelectedIndex = 1;
                }
            }
           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (mode.Equals("Add"))
            {
                cusDeptItem = new CustomerDebtItem();
                cusDeptItem.CUSTOMER_ID = CusID;
                cusDeptItem.DEBT_AMOUNT = Convert.ToInt32(this.txtAmount.Text);
                cusDeptItem.DEBT_DETAIL = this.txtDetail.Text;

                if (this.cbbDeptStatus.SelectedIndex == 0)
                {
                    cusDeptItem.CUSTOMER_DEBT_STATUS = "A";
                }
                else
                {
                    cusDeptItem.CUSTOMER_DEBT_STATUS = "I";
                }
                
                CustomerDebtInfoDAL.Insert(cusDeptItem);
                MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "เพิ่มข้อมูล");
                this.Close();
            }
            else 
            {
                cusDeptItem.DEBT_AMOUNT = Convert.ToInt32(this.txtAmount.Text);
                cusDeptItem.DEBT_DETAIL = this.txtDetail.Text;
                if (this.cbbDeptStatus.SelectedIndex == 0)
                {
                    cusDeptItem.CUSTOMER_DEBT_STATUS = "A";
                }
                else
                {
                    cusDeptItem.CUSTOMER_DEBT_STATUS = "I";
                }
                CustomerDebtInfoDAL.Update(cusDeptItem);
                MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "เปลี่ยนแปลงข้อมูล");
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
